<template>
	<view class="container">
		<u-navbar
			title="隐私设置"
			:autoBack="true"
			fixed
			placeholder
		></u-navbar>
		
		<view class="content">
			<u-cell-group title="信息显示设置">
				<u-cell
					title="个人信息显示"
					:value="personalInfoVisibility"
					isLink
					@click="showPersonalInfoPopup = true"
				></u-cell>
				<u-cell
					title="任务可见性"
					@click="toggleTaskVisibility"
				>
					<u-switch
						v-model="taskVisibilityEnabled"
						slot="right-icon"
						size="40"
						@change="toggleTaskVisibility"
					></u-switch>
				</u-cell>
				<u-cell
					title="我的手机号码"
					@click="togglePhoneVisibility"
				>
					<u-switch
						v-model="phoneVisibilityEnabled"
						slot="right-icon"
						size="40"
						@change="togglePhoneVisibility"
					></u-switch>
				</u-cell>
			</u-cell-group>
			
			<u-cell-group title="状态设置" margin-top="20">
				<u-cell
					title="在线状态显示"
					@click="toggleOnlineStatus"
				>
					<u-switch
						v-model="showOnlineStatus"
						slot="right-icon"
						size="40"
						@change="toggleOnlineStatus"
					></u-switch>
				</u-cell>
			</u-cell-group>
			
			<u-cell-group title="数据管理" margin-top="20">
				<u-cell
					title="清除浏览历史"
					isLink
					@click="clearHistory"
				></u-cell>
				<u-cell
					title="清除缓存数据"
					:value="cacheSize"
					isLink
					@click="clearCache"
				></u-cell>
			</u-cell-group>
			
			<view class="privacy-tips">
				<view class="tips-title">隐私提示</view>
				<view class="tips-content">
					<view class="tip-item">
						<text class="iconfont icon-info"></text>
						<text class="tip-text">您可以随时调整隐私设置，保护个人信息安全</text>
					</view>
					<view class="tip-item">
						<text class="iconfont icon-info"></text>
						<text class="tip-text">我们会根据您的设置保护您的隐私信息</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 个人信息可见性设置弹窗 -->
		<u-popup
			v-model="showPersonalInfoPopup"
			mode="bottom"
			borderRadius="16"
		>
			<view class="popup-content">
				<view class="popup-title">个人信息显示</view>
				<view class="popup-options">
					<u-radio-group v-model="personalInfoLevel" @change="setPersonalInfoVisibility">
						<u-radio 
							v-for="(item, index) in personalInfoOptions" 
							:key="index" 
							:name="item.value"
							:label="item.label"
							labelSize="28"
						>
						</u-radio>
					</u-radio-group>
				</view>
				<view class="popup-button">
					<u-button @click="showPersonalInfoPopup = false">确认</u-button>
				</view>
			</view>
		</u-popup>
		
		<!-- 提示弹窗 -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import tools from '@/mixin/tools.js'
	
	export default {
		mixins: [tools],
		data() {
			return {
				// 个人信息显示设置
				personalInfoLevel: 1, // 0: 所有人, 1: 仅好友, 2: 不公开
				showPersonalInfoPopup: false,
				personalInfoOptions: [
					{
						label: '所有人可见',
						value: 0
					},
					{
						label: '仅好友可见',
						value: 1
					},
					{
						label: '完全不公开',
						value: 2
					}
				],
				
				// 任务可见性
				taskVisibilityEnabled: true,
				
				// 手机号码可见性
				phoneVisibilityEnabled: false,
				
				// 在线状态
				showOnlineStatus: true,
				
				// 缓存大小
				cacheSize: '6.2MB'
			}
		},
		computed: {
			personalInfoVisibility() {
				const options = {
					0: '所有人可见',
					1: '仅好友可见',
					2: '完全不公开'
				}
				return options[this.personalInfoLevel]
			}
		},
		onLoad() {
			console.log('隐私设置页面加载')
			// 加载用户隐私设置
			this.loadPrivacySettings()
		},
		methods: {
			// 加载隐私设置
			loadPrivacySettings() {
				// 这里可以从本地存储或服务器获取用户设置
				// 示例代码，实际开发中需要替换为真实实现
				try {
					const settings = uni.getStorageSync('privacySettings')
					if (settings) {
						const data = JSON.parse(settings)
						this.personalInfoLevel = data.personalInfoLevel || 1
						this.taskVisibilityEnabled = data.taskVisibilityEnabled !== undefined ? data.taskVisibilityEnabled : true
						this.phoneVisibilityEnabled = data.phoneVisibilityEnabled !== undefined ? data.phoneVisibilityEnabled : false
						this.showOnlineStatus = data.showOnlineStatus !== undefined ? data.showOnlineStatus : true
					}
				} catch (e) {
					console.error('加载隐私设置失败', e)
				}
			},
			
			// 保存隐私设置
			savePrivacySettings() {
				const settings = {
					personalInfoLevel: this.personalInfoLevel,
					taskVisibilityEnabled: this.taskVisibilityEnabled,
					phoneVisibilityEnabled: this.phoneVisibilityEnabled,
					showOnlineStatus: this.showOnlineStatus
				}
				
				try {
					uni.setStorageSync('privacySettings', JSON.stringify(settings))
				} catch (e) {
					console.error('保存隐私设置失败', e)
				}
			},
			
			// 设置个人信息可见性
			setPersonalInfoVisibility(e) {
				this.personalInfoLevel = e
				this.savePrivacySettings()
				this.$refs.uToast.show({
					title: '个人信息可见性已更新',
					type: 'success'
				})
			},
			
			// 切换任务可见性
			toggleTaskVisibility() {
				this.taskVisibilityEnabled = !this.taskVisibilityEnabled
				this.savePrivacySettings()
				this.$refs.uToast.show({
					title: this.taskVisibilityEnabled ? '任务已设为可见' : '任务已设为不可见',
					type: 'success'
				})
			},
			
			// 切换手机号可见性
			togglePhoneVisibility() {
				this.phoneVisibilityEnabled = !this.phoneVisibilityEnabled
				this.savePrivacySettings()
				this.$refs.uToast.show({
					title: this.phoneVisibilityEnabled ? '手机号已设为可见' : '手机号已设为不可见',
					type: 'success'
				})
			},
			
			// 切换在线状态
			toggleOnlineStatus() {
				this.showOnlineStatus = !this.showOnlineStatus
				this.savePrivacySettings()
				this.$refs.uToast.show({
					title: this.showOnlineStatus ? '在线状态已显示' : '在线状态已隐藏',
					type: 'success'
				})
			},
			
			// 清除浏览历史
			clearHistory() {
				uni.showModal({
					title: '清除浏览历史',
					content: '确定要清除所有浏览历史记录吗？',
					success: (res) => {
						if (res.confirm) {
							// 模拟清除历史操作
							uni.showLoading({
								title: '清除中...'
							})
							
							setTimeout(() => {
								uni.hideLoading()
								this.$refs.uToast.show({
									title: '浏览历史已清除',
									type: 'success'
								})
							}, 1000)
						}
					}
				})
			},
			
			// 清除缓存
			clearCache() {
				uni.showModal({
					title: '清除缓存',
					content: '确定要清除所有缓存数据吗？',
					success: (res) => {
						if (res.confirm) {
							// 模拟清除缓存操作
							uni.showLoading({
								title: '清除中...'
							})
							
							setTimeout(() => {
								uni.hideLoading()
								this.cacheSize = '0KB'
								this.$refs.uToast.show({
									title: '缓存已清除',
									type: 'success'
								})
							}, 1000)
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background-color: #f8f8f8;
	}
	
	.content {
		padding: 20rpx;
	}
	
	.privacy-tips {
		margin-top: 30rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 12rpx;
		
		.tips-title {
			font-size: 28rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 20rpx;
			border-left: 8rpx solid #2979ff;
			padding-left: 20rpx;
		}
		
		.tips-content {
			.tip-item {
				display: flex;
				align-items: flex-start;
				margin-bottom: 15rpx;
				
				.iconfont {
					font-size: 28rpx;
					color: #ff9900;
					margin-right: 10rpx;
					line-height: 1.6;
				}
				
				.tip-text {
					font-size: 26rpx;
					color: #666;
					line-height: 1.6;
				}
			}
		}
	}
	
	.popup-content {
		padding: 30rpx;
		
		.popup-title {
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 30rpx;
		}
		
		.popup-options {
			margin-bottom: 40rpx;
		}
		
		.popup-button {
			padding: 0 20rpx;
		}
	}
</style> 